package com.qupeng.demo.kafka.kafkaapache.security;

import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler;
import org.apache.kafka.common.security.oauthbearer.OAuthBearerExtensionsValidatorCallback;
import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken;
import org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallback;
import org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerIllegalTokenException;
import org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerValidationResult;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.AppConfigurationEntry;
import java.util.List;
import java.util.Map;

public class OAuthTokenVerifier implements AuthenticateCallbackHandler {

    @Override
    public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
        for (Callback callback : callbacks) {
            if (callback instanceof OAuthBearerValidatorCallback cb) {
                try {
                    cb.token(validatedToken(cb.tokenValue())); // OAuthBearerValidatorCallback包含来自客户端的令牌,broker会验证这个令牌。
                } catch (OAuthBearerIllegalTokenException e) {
                    OAuthBearerValidationResult r = e.reason();
//                        cb.error(errorStatus(г), г.failureScope(), r.failure0penIdConfig());
                }
            } else if (callback instanceof OAuthBearerExtensionsValidatorCallback ecb) {
                //                        ecb.inputExtensions().map().forEach((k, v) ->
//                                ecb.valid(validateExtension(k, v))); // broker会验证来自客户端的可选扩展。
            } else {
                throw new UnsupportedCallbackException(callback);
            }
        }
    }

    private OAuthBearerToken validatedToken(String s) {
        return null;
    }

    @Override
    public void configure(Map<String, ?> configs, String saslMechanism, List<AppConfigurationEntry> jaasConfigEntries) {

    }

    @Override
    public void close() {

    }
}